package com.ly.drivingschool.service.impl;

import com.ly.drivingschool.dao.UserDao;
import com.ly.drivingschool.pojo.Pager;
import com.ly.drivingschool.pojo.User;
import com.ly.drivingschool.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户操作
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    /**
     * 汇总 注册用户数据
     * @return
     */
    @Override
    public long count() {
        return 0;
    }

    /**
     * 分页查询 用户注册信息
     * @param page
     * @param size
     * @return
     */
    @Override
    public Pager<User> findByPager(int page, int size) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", (page-1)*size);
        params.put("size", size);
        Pager<User> pager = new Pager<User>();
        List<User> list = userDao.findByPager(params);
        pager.setData(list);
        pager.setCount(userDao.count());
        return pager;
    }

    /**
     * 模糊查询
     * @param page
     * @param size
     * @param keyWork
     * @return
     */
    @Override
    public Pager<User> findByKey(int page, int size, String keyWork) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", (page-1)*size);
        params.put("size", size);
        params.put("keyWork", keyWork);
        Pager<User> pager = new Pager<User>();
        List<User> list = userDao.findByKey(params);
        pager.setData(list);
        pager.setCount(userDao.countKey(keyWork));
        return pager;
    }

    /**
     * 选择条件查询
     * @param page
     * @param size
     * @param col
     * @param code
     * @return
     */
    @Override
    public Pager<User> findByCondition(int page, int size,String col,String code) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", (page-1)*size);
        params.put("size", size);
        params.put("col", col);
        params.put("code", code);
        Pager<User> pager = new Pager<User>();
        List<User> list = userDao.findByCondition(params);
        pager.setData(list);
        pager.setCount(userDao.countXY001(col,code));
        return pager;
    }

    /**
     * 删除一条记录
     * @param userId
     * @return
     */
    @Override
    public int deleteByPrimaryKey(Integer userId) {
        int code = userDao.deleteByPrimaryKey(userId);
        return code;
    }

    /**
     * 批量删除
     * @param array
     * @return
     */
    @Override
    public int deleteByBatchUser(int[] array) {
        int code = userDao.deleteByBatchUser(array);
        return code;
    }

    /**
     * 查询一条记录
     * @param userId
     * @return
     */
    @Override
    public User selectByPrimaryKey(Integer userId) {
        User user = userDao.selectByPrimaryKey(userId);
        return user;
    }

    /**
     * 根据身份证号查询信息
     * @param idCard
     * @return
     */
    @Override
    public User selectByIdCard(String idCard) {
        User user = userDao.selectByIdCard(idCard);
        return user;
    }

    /**
     * 查询不带字典值的
     * @param userId
     * @return
     */
    @Override
    public User findByDict(Integer userId) {
        User user = userDao.findByDict(userId);
        return user;
    }

    /**
     * 更新一条记录
     * @param record
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(User record) {
        int code = userDao.updateByPrimaryKeySelective(record);
        if (code == 0) {
            return 0;
        } else {
            return 1;
        }
    }

    /**
     * 按需添加新学员
     * @param record
     * @return
     */
    @Override
    public int insertSelective(User record) {
        int code = userDao.insertSelective(record);
        if (code == 0) {
            return 0;
        } else {
            return 1;
        }
    }
}
